<?php

// 显示抢购商品

namespace app\console\controller;

use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\RushHour as RushHourModel;
use app\console\model\QuotHour as QuotHourModel;
use app\console\model\ShopGoods as ShopGoodsModel;
use think\Db;

class Limited extends Controller
{


    /**
     * 显示抢购商品列表
     * @param Request $request
     * @param LimitedModel $limited
     * @return mixed
     */
    public function index(Request $request)
    {

        // 获取参数
        $inquire = $request->param();
        $model = new RushHourModel();
        // 搜索条件
        $condition = [];
        if (!empty($inquire['title'])) $condition['sun_shop_goods.title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['quot_id'])) $condition['rush_hour.rush_id'] = $inquire['quot_id'];
        // 获取数据

        $data = $model
            ->alias('rush_hour')
            ->field('shop_goods.price goods_price,rush_hour.*')
            ->join('shop_goods','sun_rush_hour.goods_id = sun_shop_goods.id')
            ->where($condition)->paginate(12,false,['query'=>$inquire]);
        //抢购场次展示

        $quots = QuotHourModel::all();
        //halt($data);
        return $this->fetch('index', [
            'mate_title' => '限时抢购列表',
            'data'       => $data,
            'quots'      => $quots
        ]);
    }


    /**
     * 新增限时抢购商品
     * @param Request $request
     * @param RushHourModel $limited
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function create(Request $request, RushHourModel $limited, ShopGoodsModel $shopGoods)
    {
        if ($request->isPost()) {
            // 获取参数
            $data = $request->param();

            // 判断库存及价格
            $find = $shopGoods::get($data['goods_id']);

            if ($data['number'] > $find['inventory']) fail('抢购商品数量超过库存值');

            if ($data['price'] > $find['price']) fail('抢购价格高于商品正常价格');
            $same_goods_id = Db::name('rush_hour')->where('goods_id',$data['goods_id'])->find();
            if($same_goods_id) fail('抢购活动中已经有此商品');

            $data['rush_id'] = $data['automatic_time'];

            $validate = Loader::validate('Limited');
            if (!$validate->check($data)) fail($validate->getError());

            $operation = $limited->allowField(true)->save($data);

            if ($operation) {
                win('操作成功', Url::build('limited/index'));
            }

            fail('操作失败');
        }

        // 抢购时间段
        $quot_hour = QuotHourModel::all();


        return $this->fetch('', [
            'mate_title' => '新增限时抢购商品',
            'quotaTime'  => $quot_hour,
        ]);
    }


    /**
     * 更新限时抢购商品
     * @param Request $request
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function update(Request $request, RushHourModel $limited, ShopGoodsModel $shopGoods)
    {
        // 获取参数
        $id = $request->param('id');

        if ($request->isPost()) {

            // 获取参数
            $data = $request->param();

            // 判断库存及价格
            $find = $shopGoods::get($data['goods_id']);

            if ($data['number'] > $find['inventory']) fail('抢购商品数量超过库存值');

            if ($data['price'] > $find['price']) fail('抢购价格高于商品正常价格');
            //if ($find['activity'] == 1) fail('此商品已在活动中');
            $data['rush_id'] = $data['automatic_time'];
            $validate = Loader::validate('Limited');
            if (!$validate->check($data)) fail($validate->getError());

            $operation = $limited->allowField(true)->save($data, ['id' => $id]);

            if ($operation !== false) {
                win('操作成功', Url::build('limited/index'));
            }

            fail('操作失败');
        }

        // 要修改的商品
        $item = $limited::get($id);

        // 抢购时间段
        $quot_hour = QuotHourModel::all();

        return $this->fetch('create', [
            'mate_title' => '更新限时抢购商品',
            'item'       => $item,
            'quotaTime'  => $quot_hour,
        ]);
    }


    /**
     * 删除特价商品
     * @param $id
     * @param $goods_id
     * @param ShopGoodsModel $shopGoods
     * @param LimitedModel $limited
     */
    public function destroy($id, ShopGoodsModel $shopGoods, RushHourModel $limited)
    {
        $goods_id = Request::instance()->param('goods_id');
        // 将商品活动状态改变
        $ini = ['activity' => 2];
        $shopGoods->save($ini, ['id' => $goods_id]);

        if ($limited::destroy($id)) {
            win('操作成功', Url::build('limited/index'));
        }

        fail('操作失败');
    }

}